package com.xk.ucs.modbus.enums;

import com.xk.ucs.modbus.config.ModBusProperties;
import lombok.extern.slf4j.Slf4j;

/**
 * Date:2021/1/1
 * Author：01398017
 * Description: 下位机内存地址及其描述,W开头表示上位机需要主动写入
 * R开头表示上位机主动读，WR表示读写都用
 **/
@Slf4j
public enum ModbusOrderEnum {
    R_BOX_NO(getValue("boxNo"), "控制器序列号，盒子no"),

    R_SYSTEM_STATUS(getValue("systemStatus"), "为systemStatus  uint 初始化状态   1：停止状态   2：手动状态   4：自动状态    5：报警状态"),
    R_WARNING(getValue("warning"), "取字典ype为warning   bit.0：急停报警  bit.1：X伺服报警  bit.2：Z伺服报警  bit.3：安全门报警"),
    W_CONTROL_GO_BACK	(getValueInBit("controlGoBack"),"bit.0：回原点"),
    R_CONTROL_GO_BACK_COMPLETE	(getValueInBit("controlGoBackComplete"),"bit.1：回原点完成"),
    W_CONTROL_PICK_UP	(getValueInBit("controlPickUp"),"bit.2：取货操作"),
    R_W_CONTROL_PICK_UP_COMPLETE(getValueInBit("controlPickUpComplete"),"bit.3：取货完成"),
    R_W_CONTROL_PICK_UP_FAILED(getValueInBit("controlPickUpFailed"),"bit4：取货失败"),
    W_CONTROL_GOODS_SCAN	(getValueInBit("controlGoodsScan"),"bit.5:货到扫描"),
    R_W_CONTROL_GOODS_SCAN_SUCCESS(getValueInBit("controlGoodsScanSuccess"),"bit6:货到扫描成功"),
    R_W_CONTROL_GOODS_SCAN_FAILED(getValueInBit("controlGoodsScanFailed"),"bit7:货到扫描失败"),
    W_CONTROL_CONFIRM_PICK_UP(getValueInBit("controlConfirmPickUp"),"bit11:取貨完成確認"),
    W_CONTROL_RETURN_GOODS(getValueInBit("controlReturnGoods"),"bit12:开始退货"),
    W_CONTROL_RETURN_GOODS_COMPLETE(getValueInBit("controlReturnGoodsComplete"),"bit13:退货完成"),
    W_CONTROL_RETURN_GOODS_SUCCESS(getValueInBit("controlReturnGoodsSuccess"),"bit14:退货成功"),
    W_CONTROL_RETURN_GOODS_FAIL(getValueInBit("controlReturnGoodsFail"),"bit15:退货失败"),
    W_GOODS_ROW(getValue("goodsRow"), "int 发送取货排号从下到上，发送0为空，任务完成下位机自动情况数据"),
    W_GOODS_COLUMN(getValue("goodsColumn"), "int 发送取货货到号从左到右依次为1-99，最大货道号暂定99"),
    R_W_LIGHT_CONTROL(getValue("lightControl"), "int 灯光控制"),
    R_W_LIGHT_ON_DURATION(getValue("lightOnDuration"), "int 开启时间"),
    R_W_LIGHT_OFF_DURATION(getValue("lightOffDuration"), "int 关闭时间"),
    R_W_CONTROL_PICK_UP_TIME_OUT(getValueInBit("controlPickUpTimeOut"), "int 关闭时间"),
    R_BUG_CHECK(getValue("bugCheck"), "故障检查"),
    R_COLUMNS_COUNT_IN_ROW1(getValue("columnsCountInRow1"), "第一排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW2(getValue("columnsCountInRow2"), "第2排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW3(getValue("columnsCountInRow3"), "第3排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW4(getValue("columnsCountInRow4"), "第4排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW5(getValue("columnsCountInRow5"), "第5排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW6(getValue("columnsCountInRow6"), "第6排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW7(getValue("columnsCountInRow7"), "第7排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW8(getValue("columnsCountInRow8"), "第8排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW9(getValue("columnsCountInRow9"), "第9排货道数量 int"),
    R_COLUMNS_COUNT_IN_ROW10(getValue("columnsCountInRow10"), "第10排货道数量 int"),
    R_COUNT_ROWS(getValue("countRows"), "排数量 int");
    @Override
    public String

    toString() {
        return this.name() + "{" +
                "value=" + value +
                ", bit=" + bit +
                '}';
    }

    private int value;
    private String description;
    //读写的bit位
    private int bit;

    /**
     * @param description 地址描述
     */
    ModbusOrderEnum(int value, String description) {
        this.value = value;
        this.description = description;
        this.bit=-1; //未配置bit位
    }

    ModbusOrderEnum(int[] bitValue, String description) {
        this.value = bitValue[0];
        this.description = description;
        this.bit = bitValue[1];
    }


    public int getBit() {
        return bit;
    }

    public int getValue() {
        return value;
    }


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public static int getValue(String key) {
        String value = ModBusProperties.getProperties().getProperty(key);
        return Integer.parseInt(value.trim());
    }

    // [0]是地址，[1]是某个bit位为1
    public static int[] getValueInBit(String key) {
        String value = ModBusProperties.getProperties().getProperty(key);
        String[] split = value.trim().split("\\.");
        if (split.length == 2) {

            return new int[]{Integer.parseInt(split[0]), Integer.parseInt(split[1])};
        }
        throw new RuntimeException(String.format("配置 %s解析异常", key));

    }
    }
